<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="概述Three.js中有用于代表几何体的两个可用基类：THREE.Geometry与THREE.BufferGeometry。前者是旧版本中几何体的基类，后者则由新版本提供给开发者，新基类的内部数据结构组织与GPU所期待的数据结构保持一致，从而提高了运行效率。 Geometry用vertices顶点、faces面来定义物体形状，分别定义物体的顶点数组与面数组。 BufferGeometry用att">
<meta property="og:type" content="article">
<meta property="og:title" content="three.js几何体">
<meta property="og:url" content="http://example.com/2021/09/23/webgl/three.js%E5%87%A0%E4%BD%95%E4%BD%93/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="概述Three.js中有用于代表几何体的两个可用基类：THREE.Geometry与THREE.BufferGeometry。前者是旧版本中几何体的基类，后者则由新版本提供给开发者，新基类的内部数据结构组织与GPU所期待的数据结构保持一致，从而提高了运行效率。 Geometry用vertices顶点、faces面来定义物体形状，分别定义物体的顶点数组与面数组。 BufferGeometry用att">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-23T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-03T08:22:08.879Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="three.js">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/09/23/webgl/three.js%E5%87%A0%E4%BD%95%E4%BD%93/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>three.js几何体 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/23/webgl/three.js%E5%87%A0%E4%BD%95%E4%BD%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          three.js几何体
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-09-23 12:18:39" itemprop="dateCreated datePublished" datetime="2021-09-23T12:18:39+08:00">2021-09-23</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:22:08" itemprop="dateModified" datetime="2021-11-03T16:22:08+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/webgl/" itemprop="url" rel="index"><span itemprop="name">webgl</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><p>Three.js中有用于代表几何体的两个可用基类：THREE.Geometry与THREE.BufferGeometry。前者是旧版本中几何体的基类，后者则由新版本提供给开发者，新基类的内部数据结构组织与GPU所期待的数据结构保持一致，从而提高了运行效率。</p>
<p>Geometry用vertices顶点、faces面来定义物体形状，分别定义物体的顶点数组与面数组。</p>
<p>BufferGeometry用attributes分量、index索引来定义物体形状，attributes分量所存储的信息将直接送往GPU进行处理。index索引，一般不需要特意指定面，因为默认情况下position位置分量中每三个空间坐标确定一个面。但我们也可以通过index属性像Geometry类一样去指定用于组成每一个面的顶点。</p>
<h1 id="基础几何体"><a href="#基础几何体" class="headerlink" title="基础几何体"></a>基础几何体</h1><h2 id="二维几何体"><a href="#二维几何体" class="headerlink" title="二维几何体"></a>二维几何体</h2><p>THREE.PlaneGeometry</p>
<p>PlaneGeometry对象可以用来创建一个非常简单的二维矩形，有以下四种基本的属性：width（宽度）、height（高度）、widthSegments（宽度段数，指定矩形宽度应该分为几段）、heightSegments（高度段数，指定矩形宽度应该分为几段）。</p>
<p>THREE.CircleGeometry</p>
<p>PlaneGeometry对象可以用来创建一个非常简单的二维圆或者部分圆。</p>
<p>THREE.RingGeometry</p>
<p>RingGeometry对象不仅可以创建一个圆，还可以在中心定义一个孔。</p>
<p>THREE.ShapeGeometry</p>
<p>ShapeGeometry用于创建一个自定义的二维图形，通过该几何体，我们可以调用几个函数来创建自己的图形，</p>
<h2 id="三维几何体"><a href="#三维几何体" class="headerlink" title="三维几何体"></a>三维几何体</h2><p>THREE.BoxGeometry</p>
<p>简单的长方体几何体。</p>
<p>THREE.SphereGeometry</p>
<p>简单的三维球体。</p>
<p>THREE.CylinderGeometry</p>
<p>用于创建三维圆柱体。</p>
<p>THREE.ConeGeometry</p>
<p>用于创建三维圆锥体。</p>
<p>THREE.TorusGeometry</p>
<p>用于创建三维的中空环状物体，类似于甜甜圈。</p>
<p>THREE.TorusKnotGeometry</p>
<p>用于创建三维的环状扭结，类似于九连环之类的环状物体。</p>
<p>THREE.PolyhedronGeometry</p>
<p>使用该集合体，能够轻松地创建多面体，十六面或者六十面体。</p>
<h2 id="高级几何体"><a href="#高级几何体" class="headerlink" title="高级几何体"></a>高级几何体</h2><p>之前展示的基本都是three.js提供的基础几何体。这一部分主要涉足THREE.js库提供的高级几何体。</p>
<p>THREE.ConvexGeometry</p>
<p>通过THREE.ConvexGeometry，我们可以围绕一组点创建一个凸包。所谓凸包就是包围这组点的最小图形。THREE.ConvexGeometry的构造函数接受一个顶点数组作为唯一参数，同时，我们需要调用computeVertexNormals和computeFaceNormals两个函数来计算法线，因为顶点和面的法线是Three.js为物体渲染平滑的表面所必需的数据，该类需要我们主动去调用函数去执行该运算。</p>
<p>THREE.LathGeometry</p>
<p>THREE.LathGeometry允许你从一条光滑曲线创建图形，该曲线由多个点来定义，通常称作样条曲线。该样条曲线绕物体的中心Z轴旋转，得到类似花瓶或者铃铛的图形</p>
<h3 id="拉伸二维图形"><a href="#拉伸二维图形" class="headerlink" title="拉伸二维图形"></a>拉伸二维图形</h3><p>THREE.ExtrudeGeometry</p>
<p>通过THREE.ExtrudeGeometry，我们可以从一个二维图形创建一个三维图形，具体的方式是沿Z轴或者其他坐标拉伸，最终形成一个三维图形。</p>
<p>同理，除了用自己的二维图形拉伸为三维图形以外，其实SVG与创建图形的方式基本相同，因此其实可以从SVG拉伸出三维图形。SVG的含义是可缩放矢量图，基于XML标准，用于在网页上创建二维矢量图。使用d3-threeD库，我们可以自动转换这些SVG指令。</p>
<p>THREE.TubeGeometry</p>
<p>THREE.TubeGeometry沿着一条三维的样条曲线拉伸出一根管，我们可以指定一些顶点来定义路径，然后用THREE.TubeGeometry来创建这根管。</p>
<p>THREE.ParametricGeometry</p>
<p>通过THREE.ParametricGeometry，你可以创建基于等式的几何体，利用几个函数公式来创建几何体。最常见的是用于创建波浪平面的函数。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> radialWave = <span class="function"><span class="keyword">function</span> (<span class="params">u, v, optionalTarget</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> result = optionalTarget || <span class="keyword">new</span> THREE.Vector3();</span><br><span class="line">    <span class="keyword">var</span> r = <span class="number">50</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> x = <span class="built_in">Math</span>.sin(u) * r;</span><br><span class="line">    <span class="keyword">var</span> z = <span class="built_in">Math</span>.sin(v / <span class="number">2</span>) * <span class="number">2</span> * r;</span><br><span class="line">    <span class="keyword">var</span> y = (<span class="built_in">Math</span>.sin(u * <span class="number">4</span> * <span class="built_in">Math</span>.PI) + <span class="built_in">Math</span>.cos(v * <span class="number">2</span> * <span class="built_in">Math</span>.PI)) * <span class="number">2.8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> result.set( x, y, z );</span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure>

<p>正如上述的代码，只需要几行，我们就可以创建出非常有趣的几何体</p>
<h3 id="三维文本"><a href="#三维文本" class="headerlink" title="三维文本"></a>三维文本</h3><p>在Three.js中渲染文本非常容易，你所需要做的只是指定想要用的字体，以及我们在讨论THREE.ExtrudeGeometry时见到过的基本拉伸属性。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// use the defaults</span></span><br><span class="line">  <span class="keyword">var</span> stats = initStats();</span><br><span class="line">  <span class="keyword">var</span> renderer = initRenderer();</span><br><span class="line">  <span class="keyword">var</span> camera = initCamera();</span><br><span class="line">  <span class="keyword">var</span> scene = <span class="keyword">new</span> THREE.Scene();</span><br><span class="line">  initDefaultLighting(scene);</span><br><span class="line">  <span class="keyword">var</span> groundPlane = addLargeGroundPlane(scene)</span><br><span class="line">  groundPlane.position.y = <span class="number">-30</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> font_bitstream;</span><br><span class="line">  <span class="keyword">var</span> font_helvetiker_bold;</span><br><span class="line">  <span class="keyword">var</span> font_helvetiker_regular;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> step = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">var</span> text1;</span><br><span class="line">  <span class="keyword">var</span> text2;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> fontload1 = <span class="keyword">new</span> THREE.FontLoader();</span><br><span class="line">  fontload1.load( <span class="string">'../../assets/fonts/bitstream_vera_sans_mono_roman.typeface.json'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"> response </span>) </span>&#123;</span><br><span class="line">    controls.font = response;</span><br><span class="line">    font_bitstream = response;</span><br><span class="line">    controls.redraw();</span><br><span class="line">    render();</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> fontload2 = <span class="keyword">new</span> THREE.FontLoader();</span><br><span class="line">  fontload2.load( <span class="string">'../../assets/fonts/helvetiker_bold.typeface.json'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"> response </span>) </span>&#123;</span><br><span class="line">    font_helvetiker_bold = response;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> fontload3 = <span class="keyword">new</span> THREE.FontLoader();</span><br><span class="line">  fontload3.load( <span class="string">'../../assets/fonts/helvetiker_regular.typeface.json'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"> response </span>) </span>&#123;</span><br><span class="line">    font_helvetiker_regular = response;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> controls = <span class="keyword">new</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.appliedMaterial = applyMeshNormalMaterial</span><br><span class="line">    <span class="keyword">this</span>.castShadow = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">this</span>.groundPlaneVisible = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.size = <span class="number">90</span>;</span><br><span class="line">    <span class="keyword">this</span>.height = <span class="number">90</span>;</span><br><span class="line">    <span class="keyword">this</span>.bevelThickness = <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">this</span>.bevelSize = <span class="number">0.5</span>;</span><br><span class="line">    <span class="keyword">this</span>.bevelEnabled = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">this</span>.bevelSegments = <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">this</span>.bevelEnabled = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">this</span>.curveSegments = <span class="number">12</span>;</span><br><span class="line">    <span class="keyword">this</span>.steps = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">this</span>.fontName = <span class="string">"bitstream vera sans mono"</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// redraw function, updates the control UI and recreates the geometry.</span></span><br><span class="line">    <span class="keyword">this</span>.redraw = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">switch</span> (controls.fontName) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'bitstream vera sans mono'</span>: </span><br><span class="line">          controls.font = font_bitstream</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'helvetiker'</span>: </span><br><span class="line">          controls.font = font_helvetiker_regular</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'helvetiker bold'</span>: </span><br><span class="line">          controls.font = font_helvetiker_bold</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      redrawGeometryAndUpdateUI(gui, scene, controls, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">var</span> options = &#123;</span><br><span class="line">              size: controls.size,</span><br><span class="line">              height: controls.height,</span><br><span class="line">              weight: controls.weight,</span><br><span class="line">              font: controls.font,</span><br><span class="line">              bevelThickness: controls.bevelThickness,</span><br><span class="line">              bevelSize: controls.bevelSize,</span><br><span class="line">              bevelSegments: controls.bevelSegments,</span><br><span class="line">              bevelEnabled: controls.bevelEnabled,</span><br><span class="line">              curveSegments: controls.curveSegments,</span><br><span class="line">              steps: controls.steps</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">var</span> geom = <span class="keyword">new</span> THREE.TextGeometry(<span class="string">"Learning Three.js"</span>, options)</span><br><span class="line">            geom.applyMatrix(<span class="keyword">new</span> THREE.Matrix4().makeScale(<span class="number">0.05</span>,<span class="number">0.05</span>,<span class="number">0.05</span>));</span><br><span class="line">            geom.center();</span><br><span class="line">    </span><br><span class="line">            <span class="keyword">return</span> geom</span><br><span class="line">          &#125;);</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> gui = <span class="keyword">new</span> dat.GUI();</span><br><span class="line">  gui.add(controls, <span class="string">'size'</span>, <span class="number">0</span>, <span class="number">200</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'height'</span>, <span class="number">0</span>, <span class="number">200</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'fontName'</span>, [<span class="string">'bitstream vera sans mono'</span>, <span class="string">'helvetiker'</span>, <span class="string">'helvetiker bold'</span>]).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'bevelThickness'</span>, <span class="number">0</span>, <span class="number">10</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'bevelSize'</span>, <span class="number">0</span>, <span class="number">10</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'bevelSegments'</span>, <span class="number">0</span>, <span class="number">30</span>).step(<span class="number">1</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'bevelEnabled'</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'curveSegments'</span>, <span class="number">1</span>, <span class="number">30</span>).step(<span class="number">1</span>).onChange(controls.redraw);</span><br><span class="line">  gui.add(controls, <span class="string">'steps'</span>, <span class="number">1</span>, <span class="number">5</span>).step(<span class="number">1</span>).onChange(controls.redraw);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// add a material section, so we can switch between materials</span></span><br><span class="line">    gui.add(controls, <span class="string">'appliedMaterial'</span>, &#123;</span><br><span class="line">      meshNormal: applyMeshNormalMaterial, </span><br><span class="line">      meshStandard: applyMeshStandardMaterial</span><br><span class="line">    &#125;).onChange(controls.redraw)</span><br><span class="line">    </span><br><span class="line">    gui.add(controls, <span class="string">'castShadow'</span>).onChange(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;controls.mesh.castShadow = e&#125;)</span><br><span class="line">    gui.add(controls, <span class="string">'groundPlaneVisible'</span>).onChange(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;groundPlane.material.visible = e&#125;)</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    stats.update();</span><br><span class="line"></span><br><span class="line">    controls.mesh.rotation.y = step+=<span class="number">0.005</span></span><br><span class="line">    controls.mesh.rotation.x = step</span><br><span class="line">    controls.mesh.rotation.z = step</span><br><span class="line"></span><br><span class="line">    <span class="comment">// render using requestAnimationFrame</span></span><br><span class="line">    requestAnimationFrame(render);</span><br><span class="line">    renderer.render(scene, camera);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的代码首先利用Three.js提供的THREE.FondLoader类加载字体，在调用THREE.FondLoader.load()方法时，我们除了提供字体名称以外，还提供了一个临时回调函数，用于接收创建好的字体对象（即上面代码的response对象），然后将字体对象保存到变量loadedFont中，并调用render()函数来重新绘制场景，接着我们将接收到的字体对象包含在一个名为options的映射表变量中，并用它来创建THREE.TextGeometry几何体对象</p>
<h3 id="二元操作组合"><a href="#二元操作组合" class="headerlink" title="二元操作组合"></a>二元操作组合</h3><p>之前我们其实一直是利用Three.js提供的几何体进行各种操作变化，其实所能做的也会受限于THREE.js库，其实我们可以利用CSG（构造实体几何体）的技术，来将各种标准几何体组合在一起成为新的几何体。</p>
<p>intersect（相交）：使用该函数可以基于两个现有几何体的交集创建出新的几何体，两个几何体的重叠部分定义此新几何体的形状；</p>
<p>union(联合)：使用该函数可以将两个几何体联合起来创建出一个新的几何体；</p>
<p>subtract(相减)：与union相反，通过该函数，可以在第一个几何体中，移除两个几何体重叠的部分，来创建新的几何体。</p>

    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/09/23/webgl/three.js%E5%87%A0%E4%BD%95%E4%BD%93/" title="three.js几何体">http://example.com/2021/09/23/webgl/three.js几何体/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/three-js/" rel="tag"># three.js</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/09/16/webgl/three.js%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/" rel="prev" title="three.js初学">
      <i class="fa fa-chevron-left"></i> three.js初学
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/09/23/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/less%E4%B8%8Esass/" rel="next" title="less与sass">
      less与sass <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#基础几何体"><span class="nav-number">2.</span> <span class="nav-text">基础几何体</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#二维几何体"><span class="nav-number">2.1.</span> <span class="nav-text">二维几何体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三维几何体"><span class="nav-number">2.2.</span> <span class="nav-text">三维几何体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#高级几何体"><span class="nav-number">2.3.</span> <span class="nav-text">高级几何体</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#拉伸二维图形"><span class="nav-number">2.3.1.</span> <span class="nav-text">拉伸二维图形</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三维文本"><span class="nav-number">2.3.2.</span> <span class="nav-text">三维文本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二元操作组合"><span class="nav-number">2.3.3.</span> <span class="nav-text">二元操作组合</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

